﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Moon.Sql
{
    public class GridReader : IDisposable
    {
        private IDataReader reader = null;

        public GridReader(IDataReader reader)
        {
            this.reader = reader;
        }

        public T ReadFirst<T>()
        {
            var result = DataMapper.GetDataReaderFirst<T>(reader);
            reader.NextResult();
            return result;
        }

        public async Task<T> ReadFirstAsync<T>()
        {
            var result = await DataMapper.GetDataReaderFirstAsync<T>(reader);
            reader.NextResult();
            return result;
        }

        public List<T> Read<T>()
        {
            var list = DataMapper.GetDataReader<T>(reader);
            reader.NextResult();
            return list;
        }

        public async Task<List<T>> ReadAsync<T>()
        {
            var list = await DataMapper.GetDataReaderAsync<T>(reader);
            reader.NextResult();
            return list;
        }

        public void Dispose()
        {
            reader?.Close();
        }
    }
}
